package com.vegetable.modules.controller.sys;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.vegetable.common.utils.R;
import com.vegetable.modules.controller.AbstractController;
import com.vegetable.modules.entity.sys.SysMessageEntity;
import com.vegetable.modules.service.sys.SysMessageService;
import io.swagger.annotations.*;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 系统消息控制器
 * 支持消息跳转功能，用户点击消息可跳转到指定页面
 *
 * @author Qoder
 */
@RestController
@RequestMapping("/sys/message")
@AllArgsConstructor
@Api(value = "系统消息", tags = "系统消息")
public class SysMessageController extends AbstractController {
    
    private final SysMessageService sysMessageService;

    /**
     * 获取用户未读消息数量
     */
    @GetMapping("/unreadCount")
    @ApiOperation(value = "获取用户未读消息数量", notes = "获取当前登录用户未读消息的数量，用于在消息铃铛上显示未读数")
    @ApiResponses({
        @ApiResponse(code = 200, message = "成功返回未读消息数量", response = Integer.class),
        @ApiResponse(code = 401, message = "未授权访问"),
        @ApiResponse(code = 500, message = "服务器内部错误")
    })
    public R<Integer> getUnreadCount() {
        Integer count = sysMessageService.queryUnreadCount(getUserId());
        return R.data(count);
    }

    /**
     * 获取用户消息列表
     * 返回的消息中可能包含跳转链接(redirectUrl)，前端应根据此链接实现点击跳转功能
     */
    @GetMapping("/list")
    @ApiOperation(value = "获取用户消息列表", notes = "分页获取当前登录用户的消息列表，按创建时间倒序排列。返回的消息中可能包含跳转链接(redirectUrl)，前端应根据此链接实现点击跳转功能")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "page", value = "页码（从1开始）", required = true, dataType = "int", paramType = "query"),
        @ApiImplicitParam(name = "limit", value = "每页条数", required = true, dataType = "int", paramType = "query")
    })
    @ApiResponses({
        @ApiResponse(code = 200, message = "成功返回消息列表", response = IPage.class),
        @ApiResponse(code = 401, message = "未授权访问"),
        @ApiResponse(code = 500, message = "服务器内部错误")
    })
    public R<IPage<SysMessageEntity>> list(@RequestParam Integer page, @RequestParam Integer limit) {
        IPage<SysMessageEntity> pageData = sysMessageService.queryMessagePage(new Page<>(page, limit), getUserId());
        return R.data(pageData);
    }

    /**
     * 获取用户未读消息列表
     * 返回的消息中可能包含跳转链接(redirectUrl)，前端应根据此链接实现点击跳转功能
     */
    @GetMapping("/unreadList")
    @ApiOperation(value = "获取用户未读消息列表", notes = "获取当前登录用户的所有未读消息列表，按创建时间倒序排列。返回的消息中可能包含跳转链接(redirectUrl)，前端应根据此链接实现点击跳转功能")
    @ApiResponses({
        @ApiResponse(code = 200, message = "成功返回未读消息列表", response = SysMessageEntity.class, responseContainer = "List"),
        @ApiResponse(code = 401, message = "未授权访问"),
        @ApiResponse(code = 500, message = "服务器内部错误")
    })
    public R<List<SysMessageEntity>> getUnreadList() {
        List<SysMessageEntity> list = sysMessageService.queryUnreadMessageList(getUserId());
        return R.data(list);
    }

    /**
     * 标记消息为已读
     */
    @PostMapping("/read/{messageId}")
    @ApiOperation(value = "标记消息为已读", notes = "将指定消息标记为已读状态")
    @ApiImplicitParam(name = "messageId", value = "消息ID", required = true, dataType = "long", paramType = "path")
    @ApiResponses({
        @ApiResponse(code = 200, message = "成功标记消息为已读", response = Boolean.class),
        @ApiResponse(code = 400, message = "请求参数错误"),
        @ApiResponse(code = 401, message = "未授权访问"),
        @ApiResponse(code = 404, message = "消息不存在"),
        @ApiResponse(code = 500, message = "服务器内部错误")
    })
    public R<Boolean> markAsRead(@PathVariable("messageId") Long messageId) {
        boolean result = sysMessageService.markAsRead(messageId, getUserId());
        return R.data(result);
    }

    /**
     * 批量标记消息为已读
     */
    @PostMapping("/readBatch")
    @ApiOperation(value = "批量标记消息为已读", notes = "将多个消息批量标记为已读状态")
    @ApiParam(name = "messageIds", value = "消息ID数组", required = true)
    @ApiResponses({
        @ApiResponse(code = 200, message = "成功批量标记消息为已读", response = Boolean.class),
        @ApiResponse(code = 400, message = "请求参数错误"),
        @ApiResponse(code = 401, message = "未授权访问"),
        @ApiResponse(code = 500, message = "服务器内部错误")
    })
    public R<Boolean> markBatchAsRead(@RequestBody Long[] messageIds) {
        boolean result = sysMessageService.markBatchAsRead(Arrays.asList(messageIds), getUserId());
        return R.data(result);
    }

    /**
     * 删除消息
     */
    @PostMapping("/delete/{messageId}")
    @ApiOperation(value = "删除消息", notes = "删除指定的消息")
    @ApiImplicitParam(name = "messageId", value = "消息ID", required = true, dataType = "long", paramType = "path")
    @ApiResponses({
        @ApiResponse(code = 200, message = "成功删除消息", response = Boolean.class),
        @ApiResponse(code = 400, message = "请求参数错误"),
        @ApiResponse(code = 401, message = "未授权访问"),
        @ApiResponse(code = 404, message = "消息不存在"),
        @ApiResponse(code = 500, message = "服务器内部错误")
    })
    public R<Boolean> delete(@PathVariable("messageId") Long messageId) {
        boolean result = sysMessageService.deleteMessage(messageId, getUserId());
        return R.data(result);
    }

    /**
     * 批量删除消息
     */
    @PostMapping("/deleteBatch")
    @ApiOperation(value = "批量删除消息", notes = "批量删除多个消息")
    @ApiParam(name = "messageIds", value = "消息ID数组", required = true)
    @ApiResponses({
        @ApiResponse(code = 200, message = "成功批量删除消息", response = Boolean.class),
        @ApiResponse(code = 400, message = "请求参数错误"),
        @ApiResponse(code = 401, message = "未授权访问"),
        @ApiResponse(code = 500, message = "服务器内部错误")
    })
    public R<Boolean> deleteBatch(@RequestBody Long[] messageIds) {
        boolean result = sysMessageService.deleteBatchMessage(Arrays.asList(messageIds), getUserId());
        return R.data(result);
    }
}